package ar.edu.sccs.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import ar.edu.sccs.entities.Case;

public class StringHelper {

	public static String joinAndUpperCase(Case targetCase) {
		String methodAction = targetCase.getDimension(Constants.Dimension.Method.ACTION).getStringValue();
		String methodObject = targetCase.getDimension(Constants.Dimension.Method.OBJECT).getStringValue();
		return methodAction + StringUtils.capitalize(methodObject);
	}

	public static String joinAndUpperCase(String methodAction, String methodObject) {
		return methodAction + StringUtils.capitalize(methodObject);
	}

	public static String[] splitUpperCase(String source) {

		if (source == null)
			return new String[] {};

		if (source.length() == 0)
			return new String[] { "" };

		List<String> words = new ArrayList<String>();
		int wordStartIndex = 0;

		char[] letters = source.toCharArray();

		// Skip the first letter. we don't care what case it is.
		for (int i = 1; i < letters.length; i++) {
			if (Character.isUpperCase(letters[i])) {

				// Grab everything before the current index.
				words.add(new String(letters, wordStartIndex, i - wordStartIndex));
				wordStartIndex = i;
			}
		}

		// We need to have the last word.
		words.add(new String(letters, wordStartIndex, letters.length - wordStartIndex));

		// Copy to a string array.
		String[] wordArray = new String[1];
		return words.toArray(wordArray);
	}

	public static String[] splitFirstUpperCase(String source) {

		if (source == null)
			return new String[] {};

		if (source.length() == 0)
			return new String[] { "" };

		List<String> words = new ArrayList<String>();
		int wordStartIndex = 0;

		char[] letters = source.toCharArray();

		// Skip the first letter. we don't care what case it is.
		for (int i = 1; i < letters.length; i++) {
			if (Character.isUpperCase(letters[i])) {

				// Grab everything before the current index.
				words.add(new String(letters, wordStartIndex, i - wordStartIndex));
				wordStartIndex = i;
				break;
			}
		}

		// We need to have the last word.
		words.add(new String(letters, wordStartIndex, letters.length - wordStartIndex));

		// Copy to a string array.
		String[] wordArray = new String[1];
		return words.toArray(wordArray);
	}
}
